<?php

/*
 *
 *
 * This software has been made FREE by author for the interest of other developers. 
 *      I hope this will help you in coding faster and robust php code
 *            You can modify and redistribute it as you like.
 *              Your comments, suggestions and corrections 
 *                          will be highly 
 *                           APPRECIATED. 
 *                                      Author
 * @author :   Roshan Budhathoki (developer.roshan@gmail.com);
 * @URL    :   <http://www.roshanbudhathoki.com>
 *
 */

/*
 * HANDLES categories section in admin 
 */
if (! Params::getParam ( 'action' )) {
	Params::setParam ( 'action', 'index' );
}

switch (Params::getParam ( 'action' )) {
	case 'index' :
		
		if (Params::getParam ( 'category_id' )) {
			$categories = Model_Mapper_Categories::newInstance ()->getSubCategories ( Params::getParam ( 'category_id' ) );
		} else {
			$categories = Model_Mapper_Categories::newInstance ()->getTopCategories ();
		}
		
		include cp_template_path () . 'categories.php';
		break;
	
	case 'new' :
		$url = cp_url () . '?page=categories&category_id=' . $_POST ['category_id'];
		if ($_POST ['name'] == '') {
			redirect ( $url );
		}
		
		$args ['name'] = $_POST ['name'];
		$args ['description'] = $_POST ['description'];
		$args ['parent_id'] = $_POST ['category_id'];
		
		$cat = new Model_Categories ( $args );
		
		// set iamge resize size
		$width = 500; // images will be reized to 500 pixels
		$height = 500; // images will be reiszed to 500 pixels
		

		$cat->save ();
		
		if (isset ( $_FILES ['image'] ['tmp_name'] ) && $_FILES ['image'] ['tmp_name']) {
			$image_resize = new Image_Resize ();
			$image_resize->load ( $_FILES ['image'] ['tmp_name'] );
			if ($image_resize->getHeight () > $image_resize->getWidth ()) {
				$image_resize->resizeToWidth ( $width );
			} else {
				$image_resize->resizeToHeight ( $height );
			}
			$dt = getdate ();
			$ext = '';
			
			// add extension to image name
			if ($image_resize->image_type == IMAGETYPE_JPEG) {
				$ext = '.jpg';
			} else if ($image_resize->image_type == IMAGETYPE_GIF) {
				$ext = '.gif';
			} else {
				$ext = '.png';
			}
			
			$image_name = $dt [0] . '_' . $cat->getSlug () . $ext;
			
			// change to lowercase
			$image_name = strtolower ( $image_name );
			$image_save_path = content_path () . 'category_images' . DS . $image_name; // full path to the directory and image name
			$image_resize->save ( $image_save_path );
			$cat->setImage ( $image_name );
			$cat->save ();
		}
		
		redirect ( $url );
		break;
	case 'edit' :
		if (! isset ( $_GET ['category_id'] )) {
			redirect ( 'categories' );
		}
		$category = Model_Mapper_Categories::newInstance ()->getByID ( $_GET ['category_id'] );
		
		include cp_template_path () . 'category-edit.php';
		
		break;
	case 'edit_post' :
		
		$args = array ();
		$args ['category_id'] = $_POST ['category_id'];
		$fields = array ();
		
		// set iamge resize size
		$width = 500; // images will be reized to 500 pixels
		$height = 500; // images will be reiszed to 500 pixels
		

		$cat = Model_Mapper_Categories::newInstance ()->getByID ( $_POST ['category_id'] );
		$cat->setDescription ( $_POST ['description'] );
		$cat->setName ( $_POST ['name'] );
		
		if ($_FILES ['image'] ['tmp_name']) {
			$image_resize = new Image_Resize ();
			$image_resize->load ( $_FILES ['image'] ['tmp_name'] );
			if ($image_resize->getHeight () > $image_resize->getWidth ()) {
				$image_resize->resizeToWidth ( $width );
			} else {
				$image_resize->resizeToHeight ( $height );
			}
			$dt = getdate ();
			$ext = '';
			
			// add extension to image name
			if ($image_resize->image_type == IMAGETYPE_JPEG) {
				$ext = '.jpg';
			} else if ($image_resize->image_type == IMAGETYPE_GIF) {
				$ext = '.gif';
			} else {
				$ext = '.png';
			}
			
			$image_name = $dt [0] . '_' . $cat->getSlug () . $ext;
			
			// change to lowercase
			$image_name = strtolower ( $image_name );
			$image_save_path = content_path () . 'category_images' . DS . $image_name; // full path to the directory and image name
			$image_resize->save ( $image_save_path );
			$cat->setImage ( $image_name );
			$cat->save ();
		}
		
		$url = cp_url () . '?page=categories&category_id=' . $category ['parent_id'];
		redirect ( $url );
		break;
	case 'enable' :
		break;
	case 'disable' :
		break;
	case 'delete' :
		$cat=Model_Mapper_Categories::newInstance()->getByID($_GET['category_id']);
		
		
		$parent = $cat->getParent_id();
		$where='category_id='.$cat->getCategory_id();
		Model_Mapper_Categories::newInstance()->getDbTable()->delete($where);
		$url = cp_url () . '?page=categories&category_id=' . $parent;
		redirect ( $url );
		break;
}